package com.xl98.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xl98.entity.*;
import com.xl98.service.ArticleService;
import com.xl98.service.ComplainService;
import com.xl98.service.TreeholeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-05-22
 */
@RestController
@Api(value = "吐槽",tags = {"吐槽"})
@RequestMapping("/api/complain")
public class ComplainController {
    @Autowired
    private ComplainService complainService;
    @Autowired
    private ArticleService articleService;


    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @ApiOperation(value = "根据id、userId、articleId查询吐槽，或者all=true查询全部")
    public Result findComplain (@RequestBody Map<String, String> map) {
        List<Complain> complainList = complainService.findComplain(map);
        if(StringUtils.checkValNotNull(complainList)) {
            return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE, ResponseCode.SUCCESS_FIND, complainList);
        }
        else{
            return ResultFactory.buildFailResult(ResponseCode.FAIL_FIND);
        }
    }
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加内容到吐槽里")
    public Result addComplain (@RequestBody Map<String, Object> map) {
        Article article = JSON.parseObject(JSON.toJSONString(map.get("article")), Article.class);
        articleService.addArticle(article);
        Complain complain = new Complain();
        complain.setUserId((String)map.get("userId"));
        complainService.addComplain(complain);
        HashMap<String, String> complainMap = new HashMap<>();
        complainMap.put("id",complain.getId());
        complainMap.put("articleId",article.getId());
        complainService.insertArticleIdByupdate(complainMap);
        return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_INSERT);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新吐槽内容")
    public Result updateComplain (@RequestBody Map<String, Object> map) {
        Article article = JSON.parseObject(JSON.toJSONString(map.get("article")), Article.class);
        articleService.updateArticle(article);
        return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_UPDATE);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除吐槽")
    public Result delete(String id) {
        return  ResultFactory.buildSuccessResult(complainService.delete(id));
    }
}

